package com.xuxu.blog.controller;

import com.alibaba.fastjson.JSONArray;
import com.xuxu.blog.dao.ArticleDao;
import com.xuxu.blog.entity.ArticleEntity;
import com.xuxu.blog.service.ArticleService;
import com.xuxu.blog.service.elasticSearch.EsService;
import com.xuxu.common.utils.PageUtils;
import com.xuxu.common.utils.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;


/**
 * 
 *
 * @author maxuxu
 * @email maxuxu019@gmail.com
 * @date 2022-08-21 22:22:32
 */
@RestController
@RequestMapping("blog/article")
@Slf4j
@Api(value = "articleController",tags = "文章管理")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleDao articleDao;
    @Resource
    private EsService esService;

    /**
     * elasticSearch分页查询数据
     * @param page
     * @param size
     * @return
     * @throws Exception
     */
    @GetMapping("/queryByEs/{page}/{size}")
    public R queryByEs(@PathVariable("page")Integer page,
                       @PathVariable("size") Integer size) throws Exception {
        JSONArray array = esService.seelctByPage(page, size);
        return R.ok().put("data", array);
    }

    /**
     * 分页倒序查询首页文章（最新的排在前面）
     * @param page
     * @return
     */
    @PostMapping("/querypagedesc")
    public R queryByPageDesc(@RequestBody Map<String,Object> page) {
        String author = (String) page.get("author");
        String current = (String) page.get("current");
        String size = (String) page.get("size");
        return articleService.queryPageDesc(author, current, size);
    }
    /**
     * 分页倒序查询首頁文章（所有公开的文章）
     * @param page
     * @return
     */
    @PostMapping("/queryselfpagedesc")
    public R queryBySelfPageDesc(@RequestBody Map<String,Object> page) {
        String current = (String) page.get("page");
        String size = (String) page.get("limit");
        return articleService.querySelfPageDesc( current, size);
    }
    /**
     * 分页查询个人文章(个人写的博客内容)
     */
    @PostMapping("/querypageByAuthor")
    public R queryByPage(@RequestBody Map<String,Object> page) {
        PageUtils pages = articleService.queryPage2(page);
        return R.ok("发送成功").put("page",pages);
    }

    /**
     * 获取当前用户的标签（不重复）
     */
    @GetMapping("/getTag/{author}")
    public R getTags(@PathVariable("author") String author) {
        log.info("执行了方法");
        return articleService.getTags(author);
    }
    /**
     * 查询所属用户的文章内容
     */
    // @GetMapping("/list/{author}/{current}/{size}")
    // public R getArticleByAuthor(@PathVariable("author") String author,
    //                             @PathVariable("current") Integer current,
    //                             @PathVariable("size") Integer size) {
    //     log.debug("page's info is {},{},{}",author,current,size);
    //    return articleService.selectArticleByAuthor(author,current,size);
    // }

    /**
     * 分类里显示分类的结果
     *
     * @return
     */
    @GetMapping("/categoryList/{author}")
    public R getCateGoryList(@PathVariable("author") String author) {
        R r = articleService.queryByAuthor(author);
        return r;
    }

    /**
     * 分类查询文章（主要是分类用到了groupby）
     * @param author
     * @param category
     * @return
     */
    @GetMapping("/categorys/{author}/{category}")
    public R getByauthorCategory(@PathVariable("author") String author,
                                 @PathVariable("category") String category) {

        return articleService.queryByAuthorCategory(author, category);
    }
    /**
     * 点击查询博客内容功能
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    public R queryTarget(@PathVariable("id") String id) {
        ArticleEntity articleEntity = articleDao.selectById(id);
        return R.ok("查询成功").put("data", articleEntity);
    }
    /**
     * 进入文章具体页面后的最新10篇文章
     * @return
     */
    @GetMapping("/articleNew/{author}")
    public R articleList(@PathVariable("author") String author) {
        // List<ArticleEntity> list = articleService.queryList();
        // List<ArticleEntity> sortedList = list.stream().sorted(Comparator.comparing(ArticleEntity::getId).reversed()).limit(8).collect(Collectors.toList());
        return articleService.selectNewByAuthor(author);
    }

    /**
     *
     * @return
     */
    @GetMapping("/articleCount")
    public R articleCount(){
        return articleService.getCounts();
    }
    /**
     * 返回浏览量数量
     * @return
     */
    @GetMapping("/seeCount")
    public R seeCount() {
        return R.ok().put("data", 9999);
    }
    @GetMapping("/selectArticleByFlag")
    public R seelctArticleByFlag() {
        return articleService.selectArticleByFlag();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {

        ArticleEntity article = articleService.getById(id);

        return R.ok().put("article", article);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = articleService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 保存,此处还要考虑的就是可能此处有创建新的标签操作。如果传入的是自定义类型那就添加。
     */
    @PostMapping("/save")
    public R save(@RequestBody ArticleEntity article) throws IOException {
        log.debug("接收到的信息是：【{}】", article);
        articleService.SaveArticle(article);
        // esService.AddDocument(article);
        return R.ok("添加成功").put("code", 20000);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
            public R update(@RequestBody ArticleEntity article){
		articleService.updateById(article);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
            public R delete(@RequestBody Integer[] ids){
		articleService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
